import axios from "axios";
import storage from "@/utils/storage";

import qs from "qs";

// 创建一个 axios 的实例，并完成基本配置
const instance = axios.create({
	baseURL: "http://127.0.0.1:8888/",
	headers: { "Content-Type": "application/x-www-form-urlencoded" },
	transformRequest(data, headers) {
		const type = headers["Content-Type"];
		if (type === "application/x-www-form-urlencoded") return qs.stringify(data);
		return data;
	},
});


// 请求拦截器
instance.interceptors.request.use(
	(config) => {
		config.headers.Token = storage('token').get() || ""
		return config;
	},
	(error) => {
		return Promise.reject(error);
	}
);



// 响应拦截器
instance.interceptors.response.use(
	(res) => {
		if (+res.data.code === 0) {
			return res.data;
		}
		return Promise.reject(res.data.codeText);
	},
	(error) => {
		return Promise.reject(error);
	}
);

export default instance